﻿'
'  In App.xaml:
'  <Application.Resources>
'      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
'                                   x:Key="Locator" />
'  </Application.Resources>
'  
'  In the View:
'  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
'  
'  OR (WPF only):
'  
'  xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
'  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
'


Namespace ViewModel
    ''' <summary>
    ''' This class contains static references to all the view models in the
    ''' application and provides an entry point for the bindings.
    ''' <para>
    ''' Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    ''' to this locator.
    ''' </para>
    ''' <para>
    ''' In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    ''' </para>
    ''' <para>
    ''' Then use:
    ''' </para>
    ''' <code>
    ''' DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    ''' </code>
    ''' <para>
    ''' You can also use Blend to do all this with the tool's support.
    ''' </para>
    ''' <para>
    ''' See http://www.galasoft.ch/mvvm/getstarted
    ''' </para>
    ''' <para>
    ''' In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    ''' the Main property and bind to the ViewModelNameStatic property instead:
    ''' </para>
    ''' <code>
    ''' xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
    ''' DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    ''' </code>
    ''' </summary>
    Public Class ViewModelLocator
        Private Shared MainViewModel As MainViewModel

        ''' <summary>
        ''' Initializes a new instance of the ViewModelLocator class.
        ''' </summary>
        Public Sub New()
            'if (ViewModelBase.IsInDesignModeStatic)
            '{
            '    // Create design time view models
            '}
            'else
            '{
            '    // Create run time view models
            '}
            CreateMain()
        End Sub

        ''' <summary>
        ''' Gets the Main property.
        ''' </summary>
        Public Shared ReadOnly Property MainStatic() As MainViewModel
            Get
                If MainViewModel Is Nothing Then
                    CreateMain()
                End If

                Return MainViewModel
            End Get
        End Property

        ''' <summary>
        ''' Gets the Main property.
        ''' </summary>
        <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification:="This non-static member is needed for data binding purposes.")> _
        Public ReadOnly Property Main() As MainViewModel
            Get
                Return MainStatic
            End Get
        End Property

        ''' <summary>
        ''' Provides a deterministic way to delete the Main property.
        ''' </summary>
        Public Shared Sub ClearMain()
            MainViewModel.Cleanup()
            MainViewModel = Nothing
        End Sub

        ''' <summary>
        ''' Provides a deterministic way to create the Main property.
        ''' </summary>
        Public Shared Sub CreateMain()
            If MainViewModel Is Nothing Then
                MainViewModel = New MainViewModel()
            End If
        End Sub

        ''' <summary>
        ''' Cleans up all the resources.
        ''' </summary>
        Public Shared Sub Cleanup()
            ClearMain()
        End Sub
    End Class
End Namespace
